package com.dqx.haoqiu.mvp.widget;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.dqx.haoqiu.mvp.R;
import com.xmjs.baselibrary.utils.DisplayUtil;

/**
 * 错误提示对话框
 */
public class ErrorDialog extends Dialog implements View.OnClickListener {
    private TextView tv_error;
    private Button btn_error;
    private OnListener listener;

    public ErrorDialog(@NonNull Context context) {
        super(context, R.style.alert_dialog_light);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        setContentView(R.layout.dialog_error);

        setCancelable(false);
        setCanceledOnTouchOutside(false);

        //对齐窗口底部
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = DisplayUtil.getScreenW(getContext());
        lp.height = DisplayUtil.getScreenH(getContext());
        dialogWindow.setAttributes(lp);
        dialogWindow.setGravity(Gravity.BOTTOM);

        tv_error = findViewById(R.id.tv_error);
        tv_error.setOnClickListener(this);
        btn_error = findViewById(R.id.btn_error);
        btn_error.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_error:
            case R.id.tv_error:
                dismiss();
                if (listener != null)
                    listener.onReload();
                break;
        }
    }

    public ErrorDialog setErrorText(String text) {
        tv_error.setText(text);
        return this;
    }

    public ErrorDialog setBtnText(String text) {
        btn_error.setText(text);
        return this;
    }

    public ErrorDialog setBtnVisibility(int visibility) {
        btn_error.setVisibility(visibility);
        return this;
    }

    public ErrorDialog setImage(int resId) {
        tv_error.setCompoundDrawablesRelativeWithIntrinsicBounds(0, resId, 0, 0);
        return this;
    }

    public interface OnListener {
        void onReload();
    }

    public ErrorDialog setListener(OnListener listener) {
        this.listener = listener;
        return this;
    }
}
